<?php

namespace App\Http\Controllers;

use App\Models\Sanatorium;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;

class SanatoriumWxController extends Controller
{

    /*
     * 計算距離
     * */


    public function haversineDistance($lat1, $lon1, $lat2, $lon2, $unit = 'km') {
        $earthRadius = ($unit == 'km') ? 6371 : 3959; // 地球半径，单位：km或mi

        // 将经纬度转换为弧度
        $lat1 = deg2rad($lat1);
        $lon1 = deg2rad($lon1);
        $lat2 = deg2rad($lat2);
        $lon2 = deg2rad($lon2);

        // 计算差值
        $latDiff = $lat2 - $lat1;
        $lonDiff = $lon2 - $lon1;

        // Haversine 公式计算
        $a = sin($latDiff / 2) ** 2 + cos($lat1) * cos($lat2) * sin($lonDiff / 2) ** 2;
        $c = 2 * atan2(sqrt($a), sqrt(1 - $a));

        // 计算距离
        $distance = $earthRadius * $c;

        return $distance;
    }



    /**
     *  定位查詢
     */
    public function RealPositioning(Request $request){
        $rules = [
            'longitude' => ['required'],
            'latitude'      => ['required'],
        ];
        $messages = [
            'longitude.required'=>':attribute 必填',
            'latitude.required'=>':attribute 必填',
        ];
        $attributes = [
            'longitude'=>"經度",
            'latitude'=>"緯度",
        ];
        $validator = Validator::make($request->all(), $rules, $messages, $attributes);
        if ($validator->fails()) {
            return $validator->errors();
        }

        $data = $validator->valid();//获取验证通过的（有效的）数据
        $sanatoriumModel = new Sanatorium();
        $sanatoriumData = $sanatoriumModel->where(['status'=>1])->get()->toArray();
        foreach ($sanatoriumData as $k=>$v){
            $sanatoriumData[$k]['distance']=$this->haversineDistance($data['latitude'],$data['longitude'],$v['latitude'],$v['longitude']);
        }
//        排序
        $distance = array_column($sanatoriumData, 'distance');
        array_multisort($distance, SORT_ASC, $sanatoriumData);

        return Success('2001','',$sanatoriumData);
    }

    /*
     * 搜索
     * */
    public function SanatoriumSearch(Request $request){
        $sanatorium_name = $request->post('sanatorium_name');
        if(empty($sanatorium_name)){
            return Success('1001','',[]);
        }
        $sanatoriumModel = new Sanatorium();
        $sanatoriumData = $sanatoriumModel->where(['status'=>1,['sanatorium_name','like',"%$sanatorium_name%"]])->get()->toArray();
        if(empty($sanatoriumData)){
            return Success('1001','數據不存在',[]);
        }
        return Success('2001',' ',$sanatoriumData);
    }

//    記錄熱點

    public function SanatoriumRecord(Request $request){
        $id = $request->post('id');
        $record_id = $request->post('record_id');
        if(empty($id)||empty($record_id)||!is_numeric($id)||!is_numeric($record_id)){
            return Error('1001','數據不合歸');
        }
        $sanatoriumModel = new Sanatorium();
        $sanatoriumModel->SanatoriumRecordAdd($id,$record_id);
        return Success('2001','',[]);
    }

}
